<?php


namespace app\common\entity;


use think\Model;

class FarmOrders extends Model
{
    protected $table = 'farm_orders';
    const TYPE_SEND = 1;    //发货
    const TYPE_CANCEL = 2;  //取消发货
    //订单状态
    static $status = array(
        '0' => '待付款',
        '1' => '待发货',
        '2' => '待收货',
        '3' => '已完成'
    );

    public static function getAllCate()
    {
        return [
            '1' => '其他快递',
            '2' => '顺丰',
            '3' => '申通',
            '4' => '韵达快运'
        ];
    }

    /*
     * 分页订单列表
     * */
    public function getGoodsOrdersPages($search, $type)
    {
        if ($type == 'nick_name') {
            $pageParam['query']['search'] = $search;
            $pageParam['query']['type'] = $type;
            $query = new self();
            $query = $query::alias('o')->field('o.*,u.nick_name as user_nick_name,g.level_name,g.img');
            $query = $query->where('u.nick_name', $search);
            $data = $query->leftJoin("user u", 'u.id = o.user_id')
                ->leftJoin('user_package g', 'g.id = o.goods_id')
                ->order('create_time desc')
                ->paginate(5, false, $pageParam);
        } elseif ($type == 'number') {
            $map['order_number'] = $search;
            $pageParam['query']['search'] = $search;
            $pageParam['query']['type'] = $type;
            $query = new self();
            $data = $query::alias('o')->field('o.*,u.nick_name as user_nick_name,g.level_name,g.img')
                ->leftJoin("user u", 'u.id = o.user_id')
                ->leftJoin('user_package g', 'g.id = o.goods_id')
                ->where($map)
                ->order('create_time desc')
                ->paginate(5, false, $pageParam);
        } else {
            $query = new self();
            $query = $query::alias('o')->field('o.*,u.nick_name as user_nick_name,g.level_name,g.img');
            $data1 = $this->select();
            $count = count($data1);
            $data = $query->leftJoin("user u", 'u.id = o.user_id')->leftJoin('user_package g', 'g.id = o.goods_id')->order('create_time desc')->paginate(5, $count);
        }
        return $data;
    }

    /*
     *@param   user_id
     *  根据用户ID获取用户名
     * */
    public function getUserNameByUserId($user_id)
    {
        $userModel = new \app\common\entity\User();
        $userName = $userModel->getNameByUserId($user_id);
        return $userName;
    }

    /*
    *@param   user_id
    *  根据用户ID获取用户手机号
    * */
    public function getUserMobileByUserId($user_id)
    {
        $userModel = new \app\common\entity\User();
        $userName = $userModel->getMobileByUserId($user_id);
        return $userName;
    }

    /*
     * @param $key   status 的键
     * @return $statusName
     * */
    public function getStatusName($key)
    {
        $statusName = self::$status;
        return $statusName[$key];
    }

    /*
     * @param  $order_number
     * 根据订单号获取商品信息
     * */
    public function getGoods($order_number)
    {
        $goodsList = Db::field('o.*,c.*')
            ->table(['farm_orders' => 'o', 'user_package' => 'c'])
            ->where('o.goods_id=c.id and o.order_number=' . $order_number)
            ->select();

        $userModel = new \app\common\entity\Goods();
        $data = '';
        foreach ($goodsList as $k => $v) {

            $info = $userModel->getGoodsDetail($v['goods_id']);
            $pics = $userModel->getGoodsPic($v['goods_id']);
            $data .= '<p>' . $info["goods_title"] . '<img src="' . $pics[0] . '">' . '<div> 商品数量: ' . $v['goods_num'] . '</div>' . "</p>";
        }


        return $data;
    }

    /*
     * $type  发货或者取消发货的标识
     * */
    public function updateGoodsOrders(FarmOrders $goodsOrders, $data, $type)
    {
        if ($type == 1) {
            $goodsOrders->express = $data['express'];
            $goodsOrders->expressnumber = $data['expressnumber'];
            $goodsOrders->status = 2;
            $goodsOrders->send_time = time();
        } else {
            $goodsOrders->status = 1;
        }

        return $goodsOrders->save();
    }
}